package com.dkm.nettysocketdemo.myWebsite.service.impl;

import com.dkm.nettysocketdemo.myWebsite.dao.HelmetUserMapper;
import com.dkm.nettysocketdemo.myWebsite.domain.HelmetUser;
import com.dkm.nettysocketdemo.myWebsite.service.HelmetUserService;
import com.dkm.nettysocketdemo.untils.Tools;
import com.dkm.nettysocketdemo.untils.redis.RedisOperator;
import com.github.pagehelper.PageHelper;
import com.google.gson.Gson;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.ObjectUtils;

import javax.annotation.Resource;
import java.io.IOException;
import java.util.Date;
import java.util.List;

/**
 * @ClassName : HelmetUserServiceImpl  //类名
 * @Description : 用户服务接口实现类  //描述
 * @Author : zmh//作者
 * @Date: 2021-03-04 14:32  //时间
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class HelmetUserServiceImpl implements HelmetUserService {

    /**
     * 用户Mapper
     */
    @Resource
    HelmetUserMapper userMapper;

    /**
     * redis工具
     */
    @Resource
    RedisOperator redisOperator;

    Gson gson = new Gson();

    /**
     * id查询
     * @param userId 用户id
     * @return
     */
    @Override
    public HelmetUser findById(String userId) {
        HelmetUser user = gson.fromJson((String) redisOperator.get(userId),HelmetUser.class);
        if(ObjectUtils.isEmpty(user)){
            user = userMapper.selectByPrimaryKey(userId);
            redisOperator.set(userId,gson.toJson(user));
        }
        return user;
    }

    /**
     * 修改
     * @param user 需要修改的用户对象
     * @return
     */
    @Override
    public int update(HelmetUser user) {
        if (!ObjectUtils.isEmpty(user.getUserPwd())){
            user.setUserPwd(Tools.password(user.getUserPwd()));
        }
        int row = userMapper.updateByPrimaryKeySelective(user);
        if(row > 0){
            redisOperator.set(user.getUserId(),gson.toJson(user));
        }
        return row;
    }

    /**
     * 新增
     * @param user 需要新增的用户对象
     * @return
     */
    @Override
    public int insert(HelmetUser user) {
        user.setUserPwd(Tools.password(user.getUserPwd()));
        user.setIsRevision("1");
        user.setUserId(Tools.getRandomUUID());
        int row = userMapper.insertSelective(user);
        if(row>0){
            redisOperator.set(user.getUserId(),gson.toJson(user));
        }
        return row;
    }

    /**
     * 判断登录
     * @param userAccountNumber 用户名称
     * @param userPwd 用户密码
     * @return
     */
    @Override
    public HelmetUser isLogin(String userAccountNumber, String userPwd) {
        userPwd= Tools.password(userPwd);
        return userMapper.isLogin(userAccountNumber,userPwd);
    }

    @Override
    public List<HelmetUser> selectAll(String startTime, String endTime, Integer page, Integer limit, HelmetUser helmetUser) {
        PageHelper.startPage(page,limit);
        return userMapper.selectAll(startTime,endTime,helmetUser);
    }

    @Override
    public Integer rowCount(String startTime, String endTime, HelmetUser helmetUser) {
        return userMapper.rowCount(startTime,endTime,helmetUser);
    }

    /**
     * id删除
     * @param id 用户id
     * @param usName 操作人名称
     * @return 回调函数
     */
    @Override
    public Integer updateById(String id, String usName) {
        int row = userMapper.updateById(id,new Date(),usName);
        if(row > 0){
            redisOperator.del(id);
        }
        return row;
    }

    /**
     * 批量删除
     * @param ids 用户id集合
     * @param usName 操作人名称
     * @return 回调函数
     */
    @Override
    public Integer updateByAll(List<String> ids, String usName) {
        int row = userMapper.updateByAll(ids,new Date(),usName);
        if(row > 0){
            redisOperator.del(ids);
        }
        return row;
    }

    @Override
    public int mailVerify(String userId) {
        return 0;
    }

    @Override
    public HelmetUser getMail(String mail) {
        return null;
    }

    @Override
    public int updateEmail(HelmetUser myUser) {
        return 0;
    }

    @Override
    public int updatePwd(HelmetUser myUser) {
        return 0;
    }

    @Override
    public String updateFile(HelmetUser user) throws IOException {
        return null;
    }

    /**
     * 判断是否有重名的
     * @param userAccountNumber 用户账号
     * @param userId 用户id
     * @return 回调函数
     */
    @Override
    public Integer selectCount(String userAccountNumber, String userId) {
        return userMapper.selectCount(userAccountNumber,userId);
    }
}
